package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CmsArticleCategory;
import com.stylefeng.guns.modular.system.service.ICmsArticleCategoryService;

import java.util.Date;
import java.util.List;

/**
 * 新闻类别控制器
 *
 * @author fengshuonan
 * @Date 2018-10-26 15:59:24
 */
@Controller
@RequestMapping("/cmsArticleCategory")
public class CmsArticleCategoryController extends BaseController {

    private String PREFIX = "/system/cmsArticleCategory/";

    @Autowired
    private ICmsArticleCategoryService cmsArticleCategoryService;

    /**
     * 跳转到新闻类别首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "cmsArticleCategory.html";
    }

    @RequestMapping("/getTreeList")
    @ResponseBody
    public List<ZTreeNode> getTreeLit()
    {
        List<ZTreeNode> list = cmsArticleCategoryService.selectTreeList();
        return list;
    }
    /**
     * 跳转到添加新闻类别
     */
    @RequestMapping("/cmsArticleCategory_add")
    public String cmsArticleCategoryAdd() {
        return PREFIX + "cmsArticleCategory_add.html";
    }

    /**
     * 跳转到修改新闻类别
     */
    @RequestMapping("/cmsArticleCategory_update/{cmsArticleCategoryId}")
    public String cmsArticleCategoryUpdate(@PathVariable Integer cmsArticleCategoryId, Model model) {
        CmsArticleCategory cmsArticleCategory = cmsArticleCategoryService.selectById(cmsArticleCategoryId);
        model.addAttribute("item",cmsArticleCategory);
        LogObjectHolder.me().set(cmsArticleCategory);
        return PREFIX + "cmsArticleCategory_edit.html";
    }

    /**
     * 获取新闻类别列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return cmsArticleCategoryService.selectList(null);
    }

    /**
     * 新增新闻类别
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsArticleCategory cmsArticleCategory) {
        cmsArticleCategory.setGmtCreate(new Date());
        cmsArticleCategory.setGmtModified(new Date());
        cmsArticleCategory.setPid(0);
        cmsArticleCategory.setGmtUser(ShiroKit.getUser().account);

        cmsArticleCategoryService.insert(cmsArticleCategory);
        return SUCCESS_TIP;
    }

    /**
     * 删除新闻类别
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer cmsArticleCategoryId) {
        cmsArticleCategoryService.deleteById(cmsArticleCategoryId);
        return SUCCESS_TIP;
    }

    /**
     * 修改新闻类别
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CmsArticleCategory cmsArticleCategory) {
        cmsArticleCategoryService.updateById(cmsArticleCategory);
        return SUCCESS_TIP;
    }

    /**
     * 新闻类别详情
     */
    @RequestMapping(value = "/detail/{cmsArticleCategoryId}")
    @ResponseBody
    public Object detail(@PathVariable("cmsArticleCategoryId") Integer cmsArticleCategoryId) {
        return cmsArticleCategoryService.selectById(cmsArticleCategoryId);
    }
}
